const PENDING = 'pending'
const FULFILLED = 'fulfilled'
const REJECTED = 'rejected'

class MyPromise {
  constructor(executor) {
    executor(this.resolove, this.reject)
  }
  status = PENDING
  value = undefined
  reson = undefined
  successCallback = undefined
  failCallback = undefined
  resolove = (value) => {
    if (this.status !== PENDING) return
    this.status = FULFILLED
    this.value = value
    this.successCallback && this.successCallback(this.value)
  }
  reject = (reson) => {
    if (this.status !== PENDING) return
    this.status = REJECTED
    this.reson = reson
    this.failCallback && this.failCallback(this.reson)
  }
  then(successCallback, failCallback) {
    if (this.status === FULFILLED) {
      successCallback(this.value)
    } else if (this.status === REJECTED) {
      failCallback(this.reson)
    } else {
      this.successCallback = successCallback
      this.failCallback = failCallback
    }
  }
}

module.exports = MyPromise